package com.wlgzs.booksystem.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.wlgzs.booksystem.entity.Collection;
import com.wlgzs.booksystem.enums.Result;
import com.wlgzs.booksystem.enums.ResultCodeEnum;
import com.wlgzs.booksystem.service.CollectionService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author wlgzs-sjl
 * @since 2020-11-17
 */
@RestController
@RequestMapping("/collection")
@Api(tags = "收藏管理")
public class CollectionController {
    @Resource
    CollectionService collectionService;

    @PostMapping
    @ApiOperation(value = "添加收藏",httpMethod = "POST", response = Result.class)
    public Result addCollection(Collection collection){
        if (collectionService.save(collection)){
            return new Result(ResultCodeEnum.SAVE);
        }else {
            return new Result(ResultCodeEnum.UNSAVE);
        }
    }

    @DeleteMapping
    @ApiOperation(value = "取消收藏",httpMethod = "DELETE", response = Result.class)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userId", value = "用户id", required = true, dataType = "int"),
            @ApiImplicitParam(name = "bookId", value = "书本id", required = true, dataType = "int")
    })
    public Result deleteCollection(Integer userId,Integer bookId){
        QueryWrapper wrapper=new QueryWrapper();
        wrapper.eq("user_id",userId);
        wrapper.eq("book_id",bookId);
        Collection collection=collectionService.getOne(wrapper);
        System.out.println(collection);
        if (collectionService.removeById(collection.getCollectionId())){
            return new Result(ResultCodeEnum.DELETE);
        }else {
            return new Result(ResultCodeEnum.UNDELETE);
        }
    }
}

